package com.tuzhi.auth.sys.scurity.impl;


import com.tuzhi.auth.sys.scurity.TokenStoreService;
import com.tuzhi.base.redis.MyRedisTemplate;
import com.tuzhi.common.domain.LoginUserBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TokenStoreServiceImpl implements TokenStoreService {

    @Autowired
    MyRedisTemplate redisCache;

    @Override
    public LoginUserBO getLoginUser(String token) {
        return redisCache.getCacheObject(token);
    }

    @Override
    public void delLoginUser(String key) {
        redisCache.del(key);
    }

    @Override
    public void storeLoginUser(LoginUserBO loginUserBO) {
        int expireTime = getExpireTime(loginUserBO);
        String key = getTokenKey(loginUserBO.getJti());
        redisCache.setCacheObject(key, loginUserBO, expireTime, TimeUnit.SECONDS);
    }

    @Override
    public <T> void setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {

        redisCache.setCacheObject(key, value, timeout, timeUnit);
    }

    @Override
    public String getTokenKey(String uuid) {
        return null;
    }

    static private int getExpireTime(LoginUserBO loginUserBO) {

        long ts = loginUserBO.getExpireTime() - loginUserBO.getLoginTime();
        ts = ts / 1000;
        return Long.valueOf(ts).intValue();
    }

    public static void main(String[] args) {
        LoginUserBO loginUserBO = new LoginUserBO();
        loginUserBO.setLoginTime(1676448276046l);
        loginUserBO.setExpireTime(1676450076046l);
        System.out.printf("aa" + getExpireTime(loginUserBO));

    }
}

